﻿using System;
using System.ComponentModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Phone.Tasks;
using WinSource.Model;
using WinSource.Services;

namespace WinSource.ViewModel
{
    public class SavedArticlePropertyChangedMessage
    {
    }

    public class SavedArticleViewModel : ViewModelBase
    {
        #region Fields

        private readonly DataService _dataService;

        #endregion

        #region Constructor

        public SavedArticleViewModel(DataService dataService)
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                _dataService = dataService;

                _dataService.OnSavedArticleChanged += DataServiceOnSavedArticleChanged;

                Article = _dataService.CurrentSavedArticle;

                PreviousArticleCommand = new RelayCommand(MoveToPreviousArticle, () => CanMoveToPreviousArticle);
                NextArticleCommand = new RelayCommand(MoveToNextArticle, () => CanMoveToNextArticle);
                ShareEmailArticleCommand = new RelayCommand(ShareEmailArticle);
                ShareArticleCommand = new RelayCommand(ShareArticle);
                DeleteSavedArticleCommand = new RelayCommand(DeleteSavedArticle);
            }
        }

        #endregion

        #region Properties

        public Article Article { get; set; }

        public RelayCommand PreviousArticleCommand { get; private set; }
        public RelayCommand NextArticleCommand { get; private set; }
        public RelayCommand ShareEmailArticleCommand { get; private set; }
        public RelayCommand ShareArticleCommand { get; private set; }
        public RelayCommand DeleteSavedArticleCommand { get; private set; }

        #endregion

        #region Commands

        private bool CanMoveToPreviousArticle
        {
            get { return _dataService.CanMoveToPreviousSavedArticle; }
        }

        private bool CanMoveToNextArticle
        {
            get { return _dataService.CanMoveToNextSavedArticle; }
        }

        private void MoveToPreviousArticle()
        {
            _dataService.MoveToPreviousSavedArticle();
        }

        private void MoveToNextArticle()
        {
            _dataService.MoveToNextSavedArticle();
        }

        private void ShareEmailArticle()
        {
            try
            {
                EmailComposeTask emailComposeTask = new EmailComposeTask
                                                        {
                                                            Subject = string.Format("WinSource: {0}", Article.Title),
                                                            Body =
                                                                string.Format(
                                                                    "Hi,\n\nThis article will interest you: {0}\n\n{1}\n\nSent by the WinSource application for Windows Phone",
                                                                    Article.Title, Article.Url)
                                                        };
                emailComposeTask.Show();
            }
            catch
            {
            }
        }

        private void ShareArticle()
        {
            try
            {
                ShareLinkTask shareLinkTask = new ShareLinkTask
                                                  {
                                                      Title = "Read this article!",
                                                      LinkUri = new Uri(Article.Url, UriKind.Absolute),
                                                      Message = Article.Url
                                                  };

                shareLinkTask.Show();
            }
            catch
            {
            }
        }

        private void DeleteSavedArticle()
        {
            _dataService.DeleteSavedArticle(Article);
            
            DeleteSavedArticleCommand.RaiseCanExecuteChanged();
        }

        #endregion

        #region Event Handlers

        private void DataServiceOnSavedArticleChanged(object sender, ArticleChangedEventArgs articleChangedEventArgs)
        {
            Article = articleChangedEventArgs.Article;

            Messenger.Default.Send(new SavedArticlePropertyChangedMessage());

            NextArticleCommand.RaiseCanExecuteChanged();
            PreviousArticleCommand.RaiseCanExecuteChanged();
            DeleteSavedArticleCommand.RaiseCanExecuteChanged();
        }

        #endregion
    }
}